package com.house365.core.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.house365.core.R;

/**
 *@author py
 *@date 2012-7-18
 *@comment
 */
public class Switch extends ImageView {
    private static final int TEXT_SIZE = 12;
    private OnCheckedChangeListener mOnCheckedChangeListener;

    private boolean mChecked;
    
    private boolean drawText = false;

    private Paint mPaint;


    private String checkedString, unCheckedString;

    private int onImage,offImage;
    public interface OnCheckedChangeListener {
        public void onCheckedChanged(Switch switchView, boolean isChecked);
    }

    public Switch(Context context) {
        this(context, null);
    }

    public Switch(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Switch(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        
        TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.Switch);
		onImage = a.getResourceId(R.styleable.Switch_onImage, -1);
		offImage = a.getResourceId(R.styleable.Switch_offImage, -1);
		drawText = a.getBoolean(R.styleable.Switch_drawText, false);
		if(drawText){
			checkedString =context.getResources().getString(a.getResourceId(R.styleable.Switch_onText, -1));
			unCheckedString =context.getResources().getString(a.getResourceId(R.styleable.Switch_offText, -1));
		}
		
		a.recycle();
        
        setChecked(false);//��ʼ���ؼ�Ϊ�ص�״̬
        configPaint();//���û������ԣ�ȥ���ܶȣ�ֱ��������12��ʾ��
    }

    private void configPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//ȥ���־��
      //  mPaint.setTextSize(TEXT_SIZE * mDensity);
        mPaint.setTextSize(TEXT_SIZE);
        mPaint.setColor(Color.WHITE);//���������������������ɫ
    }

    public boolean isDrawText() {
		return drawText;
	}

	public void setDrawText(boolean drawText) {
		this.drawText = drawText;
	}

	public void setChecked(boolean checked) {
        mChecked = checked;
        if (mChecked) {
            setImageResource(onImage);
            setScaleType(ScaleType.FIT_XY);
        } else {
            setImageResource(offImage);
            setScaleType(ScaleType.FIT_XY);
        }
        postInvalidate();
            //��http://blog.csdn.net/sdhjob/article/details/6512973
            //View��invalidate��postInvalidate���Ǹ��½��棬��ͬ����
            //invalidate�봴��VIEW������ͬһ���߳���ִ�еģ������̣߳�����ڷ����߳���ִ�еĻ���Ҫ���Handlerʹ��
            //��postInvalidate�����ڷ����߳��и���UI����

    }

    public boolean isChecked() {
        return mChecked;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(drawText){
        	 if (mChecked) {
                 // draw checked
             	canvas.drawText(checkedString, (getWidth()*0.6f), getHeight()*0.7f, mPaint);
             } else { 
                 // draw unchecked
             	canvas.drawText(unCheckedString, getWidth()*0.1f , getHeight()*0.7f, mPaint);
             }
        }
       
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_UP:
                setChecked(!mChecked);
                if (mOnCheckedChangeListener != null) {
                    mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
                }
                break;
            default:
                // Do nothing
                break;
        }

        return true;
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        mOnCheckedChangeListener = listener;
    }

}
